<template>
  <div class="space-usage-analyze">
    <a-flex gap="middle">
      <a-card title="存储空间" style="width: 50%">
        <div style="height: 295px; text-align: center">
          <div style="margin-bottom: 25px" />
          <h3 style="font-size: 24px">
            {{ formatSize(data.usedSize) }} /
            {{ data.maxSize ? formatSizeNoDecimalFraction(data.maxSize) : '无限制' }}
          </h3>
          <div style="margin-bottom: 40px" />
          <a-popover v-if="data.maxSize" trigger="hover" placement="top" :color="'black'">
            <template #content>
              <div :style="{ color: 'white' }">
                已使用容量：{{ formatSize(data.usedSize) }} /
                {{ formatSizeNoDecimalFraction(data.maxSize) }}
              </div>
              <div :style="{ color: 'white' }">
                已使用 {{ ((data.usedSize / data.maxSize) * 100).toFixed(4) }} % 的空间
              </div>
            </template>
            <template #title>
              <div :style="{ color: 'white' }">可使用空间容量：</div>
            </template>
            <a-progress
              v-if="data.maxSize"
              type="dashboard"
              :percent="data.sizeUsageRatio"
              :size="170"
            />
          </a-popover>
          <a-progress
            v-if="!data.maxSize"
            type="dashboard"
            :percent="100"
            :format="() => '无限制'"
            :size="170"
          />
        </div>
      </a-card>
      <a-card title="图片数量" style="width: 50%">
        <div style="margin-bottom: 25px" />
        <div style="height: 295px; text-align: center">
          <h3 style="font-size: 24px">
            {{ data.usedCount }} 张 / {{ data.maxCount ?? '无限制' }}
            {{ data.maxCount ? '张' : '' }}
          </h3>
          <div style="margin-bottom: 40px" />
          <a-popover v-if="data.maxSize" trigger="hover" placement="top" :color="'black'">
            <template #content>
              <div :style="{ color: 'white' }">
                已上传图片：{{ data.usedCount }} 张 / {{ data.maxCount }} 张
              </div>
              <div :style="{ color: 'white' }">
                可上传图片： {{ data.maxCount - data.usedCount }} 张
              </div>
            </template>
            <template #title>
              <div :style="{ color: 'white' }">可上传图片数量：</div>
            </template>
            <a-progress
              v-if="data.maxCount"
              type="dashboard"
              :percent="data.countUsageRatio"
              :size="170"
            />
          </a-popover>
          <a-progress
            v-if="!data.maxCount"
            type="dashboard"
            :percent="100"
            :format="() => '无限制'"
            :size="170"
          />
        </div>
      </a-card>
    </a-flex>
  </div>
</template>

<script setup lang="ts">
import { ref, watchEffect } from 'vue'
import { getSpaceUsageAnalyzeUsingPost } from '@/api/spaceAnalyzeController.ts'
import { message } from 'ant-design-vue'
import { formatSize } from '../../utils/index.ts'
import { formatSizeNoDecimalFraction } from '../../utils/index.ts'

interface Props {
  queryAll?: boolean
  queryPublic?: boolean
  spaceId?: string
}

const props = withDefaults(defineProps<Props>(), {
  queryAll: false,
  queryPublic: false,
})

// 图标数据
const data = ref<API.SpaceUsageAnalyzeResponse>({})
// 显示处于加载状态，进入页面就显示加载中
const loading = ref(true)
// 获取数据
const fetchData = async () => {
  const res = await getSpaceUsageAnalyzeUsingPost({
    queryAll: props.queryAll,
    queryPublic: props.queryPublic,
    // @ts-ignore
    spaceId: props.spaceId,
  })
  if (res.data.code === 0 && res.data.data) {
    // @ts-ignore
    data.value = res.data.data
  } else {
    message.error('空间资源使用分析失败，' + res.data.message)
  }
  loading.value = false
}

/**
 * 参数改变时，就要触发函数，重新获取数据
 */
watchEffect(() => {
  fetchData()
})
</script>

<style scoped></style>
